<?php
# Sample that explains you how to use the PHP RawsClient lib to communicate with the RAMS 'traffic' resource
#
# Copyright (C) 2010 rambla.be

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_once '../RawsClient/Raws/Rams.php';

# Provide your own credentials here
define('BASE', 'http://rams.mon01.rambla.be/');
define('USER', 'XXX');
define('PWD', 'XXX');

try 
{
  # Initialize the HTTP connection object
  # -------------------------------------
  $rams = new Rams(USER, PWD, BASE);

  # Example 1: getting all traffic data for a single file
  # -----------------------------------------------------
  echo "-> Retrieving a RAMS traffic feed for the file located at '/animals/snake.mp4':\n";
  
  # Pass a Query-String to get the total traffic for the period starting September 1 2010 and ending October 30 2010
  $query = new Rams_TrafficQuery();
  $query->setFrom("2010-09-01");
  $query->setUntil("2010-10-30");
  
  # Get the feed from RAMS
  # Note: since we haven't passed a 'type' query-string argument, a separate entry for each of the available traffic types will be returned.
  # All entries will have their 'kind' attribute set to 'file' (since the request is directed at a file)
  $feed = $rams->getTrafficFeed("/animals/snake.mp4", $query);
  foreach ($feed as $entry) {
    # Retrieve the entry element's "kind" and "type" attributes
    echo "Found " . $entry->type . " entry with kind = " . $entry->kind . "\n";
    # Retrieve the value of the params child elements
    echo "Path: " . $entry->content->params->path->text . "\n";
    echo "Volume: " . $entry->content->params->volume->text . "\n";
    echo "Hits: " . $entry->content->params->hits->text . "\n";
    echo "Updated: " . $entry->content->params->updated->text . "\n";
    echo "Last Request: " . $entry->content->params->last_request->text . "\n";
    echo "Seconds Average: " . $entry->content->params->sec_avg->text . "\n";
    echo "Seconds Total: " . $entry->content->params->sec_total->text . "\n";
    echo "Pct 0_10: " . $entry->content->params->pct_0_10->text . "\n";
    echo "Pct 10_20: " . $entry->content->params->pct_10_20->text . "\n";
    echo "Pct 20_30: " . $entry->content->params->pct_20_30->text . "\n";
    echo "Pct 30_40: " . $entry->content->params->pct_30_40->text . "\n";
    echo "Pct 40_50: " . $entry->content->params->pct_40_50->text . "\n";
    echo "Pct 50_60: " . $entry->content->params->pct_50_60->text . "\n";
    echo "Pct 60_70: " . $entry->content->params->pct_60_70->text . "\n";
    echo "Pct 70_80: " . $entry->content->params->pct_70_80->text . "\n";
    echo "Pct 80_90: " . $entry->content->params->pct_80_90->text . "\n";
    echo "Pct 90_100: " . $entry->content->params->pct_90_100->text . "\n";
    echo "Pct Unknown: " . $entry->content->params->pct_unknown->text . "\n";
    echo "time0: " . $entry->content->params->time0->text . "\n";
    echo "time1: " . $entry->content->params->time1->text . "\n";
    echo "time2: " . $entry->content->params->time2->text . "\n";
    echo "time3: " . $entry->content->params->time3->text . "\n";
    echo "time4: " . $entry->content->params->time4->text . "\n";
    echo "time5: " . $entry->content->params->time5->text . "\n";
    echo "time6: " . $entry->content->params->time6->text . "\n";
    echo "time7: " . $entry->content->params->time7->text . "\n";
    echo "time8: " . $entry->content->params->time8->text . "\n";
    echo "\n";
  }

  # Example 2: getting percentage viewed stats for all files inside a directory
  # ---------------------------------------------------------------------------
  echo "-> Retrieving the user-percentage stats for all rtmp traffic to the files inside the directory '/animals/rabbits':\n";

  # Pass a Query-String
  $query = new Rams_TrafficQuery();

  # Tell RAMS that we are requesting all files inside the directory
  $query->setKind("file");
  # Note: if you would like to request all files below a directory (= recursively), use kind=recursive instead
  # $query->setKind("recursive");

  # Tell RAMS that we only want results for rtmp traffic
  $query->setType("rtmp");
  # Tell RAMS to get the total traffic for a period starting at noon today and ending at 8 PM today
  $query->setYear("curr");
  $query->setMonth("curr");
  $query->setDay("curr");
  $query->setHour("12-20");
  # Tell RAMS that we want up to 50 entries in a single response
  $query->setPaginateBy("50");

  # get the first feed (page 1, containing max 50 entries)
  $feed = $rams->getTrafficFeed('/animals/rabbits', $query);
  while($feed) # start a while loop, in case there are more than 50 results => multiple pages
  { 
    foreach ($feed as $entry) {
      # Retrieve the entry element's "kind" and "type" attributes
      echo "Found " . $entry->type . " entry with kind = " . $entry->kind . "\n";
      echo "Pct 0_10: " . $entry->content->params->pct_0_10->text . "\n";
      echo "Pct 10_20: " . $entry->content->params->pct_10_20->text . "\n";
      echo "Pct 20_30: " . $entry->content->params->pct_20_30->text . "\n";
      echo "Pct 30_40: " . $entry->content->params->pct_30_40->text . "\n";
      echo "Pct 40_50: " . $entry->content->params->pct_40_50->text . "\n";
      echo "Pct 50_60: " . $entry->content->params->pct_50_60->text . "\n";
      echo "Pct 60_70: " . $entry->content->params->pct_60_70->text . "\n";
      echo "Pct 70_80: " . $entry->content->params->pct_70_80->text . "\n";
      echo "Pct 80_90: " . $entry->content->params->pct_80_90->text . "\n";
      echo "Pct 90_100: " . $entry->content->params->pct_90_100->text . "\n";
      echo "Pct Unknown: " . $entry->content->params->pct_unknown->text . "\n";
    }
    # Check if RAMS has any more results left
    echo "URL at which next page can be retrieved : " . $rams->getNextLink($feed) . "\n";
    # Check how many pages are available
    echo "Number of the last page can be retrieved : " . $rams->getLastNr($feed) . "\n";
    echo "URL at which last page can be retrieved : " . $rams->getLastLink($feed) . "\n";
    # get next feed, by sending a new request to the next link inside this page
    $feed = $rams->getNextFeed($feed);
  }

  # Example 3: getting traffic volume and hits totals for all sub-directories
  # -------------------------------------------------------------------------
  echo "-> Retrieving all http and rtmp traffic for the top directories of your user-account (= directories located directly under your root dir)\n";

  # Pass a Query-String
  $query = new Rams_TrafficQuery();
  # Tell RAMS that we are requesting the totals for all sub-directories
  $query->setKind("dir");
  # Tell RAMS that we only want http, rtmp and cupertino traffic entries
  $query->setType("http,rtmp,cupertino");
  # Tell RAMS to get the total traffic for the period starting September 1 2010 and ending October 30 2010
  $query->setFrom("2010-09-01");
  $query->setUntil("2010-10-30");
  # Tell RAMS that we want up to 100 entries (= maximum) in a single response
  $query->setPaginateBy("100");

  # get the first feed (page 1, containing max 100 entries)
  $feed = $rams->getTrafficFeed('/animals/rabbits', $query);
  while($feed) # start a while loop, in case there are more than 100 results => multiple pages
  { 
    foreach ($feed as $entry) {
      # Retrieve the entry element's "kind" (= always 'dir) and "type" (= can be 'http', 'rtmp' or 'cupertino') attributes
      echo "Found " . $entry->type . " entry with kind = " . $entry->kind . "\n";
      # Retrieve the value of the params child elements
      echo "Path: " . $entry->content->params->path->text . "\n"; # path to the sub-directory
      echo "Volume: " . $entry->content->params->volume->text . "\n"; # total volume for all traffic to files inside the sub-directory (= recursive)
      echo "Hits: " . $entry->content->params->hits->text . "\n"; # total number of hits for all files inside the sub-directory (= recursive)
    }
    # check if RAMS has any more results left
    echo "URL at which next page can be retrieved : " . $rams->getNextLink($feed) . "\n";
    # get next feed, by sending a new request to the next link inside this page
    $feed = $rams->getNextFeed($feed);
  }

  # Example 4: getting traffic volume and hits totals for a single directory
  # -------------------------------------------------------------------------
  echo "-> Retrieving all http and ftp traffic for the directory located at '/animals/rabbits'\n";

  # Pass a Query-String
  $query = new Rams_TrafficQuery();
  # Tell RAMS that we are requesting the totals for a single directory
  $query->setKind("root");
  # Tell RAMS that we want all traffic types available
  $query->setType("all");
  # Tell RAMS to get the total traffic for the period starting September 1 2010 and ending October 30 2010
  $query->setFrom("2010-09-01");
  $query->setUntil("2010-10-30");
  
  # Get the feed from RAMS
  $feed = $rams->getTrafficFeed("/animals/rabbits", $query);
  foreach ($feed as $entry) {
    # Retrieve the entry element's "kind" (= always 'dir) and "type" (= can be any type name, or 'all' for the entry that contains the sum of all types) attributes
    echo "Found " . $entry->type . " entry with kind = " . $entry->kind . "\n";
    # Retrieve the value of the params child elements
    echo "Path: " . $entry->content->params->path->text . "\n"; # is '/animals/rabbits'
    echo "Volume: " . $entry->content->params->volume->text . "\n"; # total volume for all traffic of this type for all files inside this directory (= recursive)
    echo "Hits: " . $entry->content->params->hits->text . "\n"; # total number of hits for this type and for all files inside this directory (= recursive)
  }


}
catch(Zend_Gdata_App_Exception $e) {
    # Report the exception to the user
    echo "\nCaught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
    # get the HTTP status code
    echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
    echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
    # get the raws:error elements
    $rawsResponse = Raws::parseRawsResponse($e->getResponse());
    echo "Raws Code: " . $rawsResponse->getCode() . "\n";
    echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
    $reasons = $rawsResponse->getReasons();
    foreach ($reasons as $reason) {
      echo "Raws Reason: " . $reason . "\n";
    }
    $details = $rawsResponse->getDetails();
    foreach ($details as $key => $value) {
      echo "Raws Detail: " . $key . " -> " . $value . "\n";
    }
}
catch (Zend_Exception $e) {
    echo "Caught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
}
